package com.codeoss.framework.workflow.def;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.codeoss.framework.expression.Expression;

public class WorkflowDef extends WorkflowElementDef {
	private List<NodeDef> nodeList = new ArrayList<NodeDef>();
	private List<LineDef> lineList = new ArrayList<LineDef>();
	private Map<String, NodeDef> nodeMap = new HashMap<String, NodeDef>();

	private StartNodeDef startNode;
	private String code;
	private Expression startEventExpression;
	private Expression endEventExpression;
	private String name;
	private Expression documentExpression;
	private String formCode;
	private String mobileFormCode;
	public void addNode(NodeDef nodeDef) {
		nodeList.add(nodeDef);
		nodeMap.put(nodeDef.getId(), nodeDef);
		if (nodeDef instanceof StartNodeDef) {
			if (startNode != null) {
				throw new RuntimeException("只能存在一个开始节点");
			}
			startNode = (StartNodeDef) nodeDef;
		}
	}

    public StartNodeDef getStartNode() {
		return startNode;
	}

	public void setStartNode(StartNodeDef startNode) {
		this.startNode = startNode;
	}

	public List<NodeDef> getNodeList() {
		return nodeList;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public void addLine(LineDef lineDef) {
		lineList.add(lineDef);
	}

	public List<LineDef> getLineList() {
		return lineList;
	}

	public NodeDef getNodeDefById(String id) {
		return nodeMap.get(id);
	}

	public Expression getStartEventExpression() {
		return startEventExpression;
	}

	public void setStartEventExpression(Expression startEventExpression) {
		this.startEventExpression = startEventExpression;
	}

	public Expression getEndEventExpression() {
		return endEventExpression;
	}

	public void setEndEventExpression(Expression endEventExpression) {
		this.endEventExpression = endEventExpression;
	}

	public LineDef findLineDef(String fromNodeId, String toNodeId) {
		for (LineDef lineDef : lineList) {
			if (lineDef.getFromNodeId().equals(fromNodeId) && lineDef.getToNodeId().equals(toNodeId)) {
				return lineDef;
			}
		}
		return null;
	}

	public NodeDef findNodeDef(String nodeId) {
		for (NodeDef nodeDef : nodeList) {
			if (nodeDef.getId().equals(nodeId)) {
				return nodeDef;
			}
		}
		return null;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Expression getDocumentExpression() {
		return documentExpression;
	}

	public void setDocumentExpression(Expression documentExpression) {
		this.documentExpression = documentExpression;
	}

	public String getFormCode() {
		return formCode;
	}

	public void setFormCode(String formCode) {
		this.formCode = formCode;
	}

	public String getMobileFormCode() {
		return mobileFormCode;
	}

	public void setMobileFormCode(String mobileFormCode) {
		this.mobileFormCode = mobileFormCode;
	}

	@Override
	public String getType() {
		// TODO Auto-generated method stub
		return "workflow";
	}
}
